home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / ddj0897.zip / DYN401.ZIP / threads / pipe.c < prev    next >
C/C++ Source or Header  |  1997-04-16  |  8KB  |  376 lines

  1.  
  2.  
  3. /*  Copyright (c) 1993-1996 Algorithms Corporation  */
  4. /*  All rights reserved.  */
  5.  
  6.  
  7.  
  8.  
  9. /*  This file automatically generated by dpp - do not edit  */
  10.  
  11. #define    DPP_STRATEGY    2
  12. #define    DPP_FASTWIDE    0
  13.  
  14.  
  15.  
  16. #line 18 "pipe.d"
  17. #include <string.h> 
  18.  
  19. #define    CLASS    Pipe_c
  20. #define    ivType    Pipe_iv_t
  21.  
  22. #include "generics.h"
  23.  
  24. object    Pipe_c;
  25.  
  26.  
  27. #line 28 "pipe.c"
  28. typedef struct  _Pipe_iv_t  {
  29.     object iObj;
  30.     char * iName;
  31.     char * iBuf;
  32.     int iBufsiz;
  33.     char * iWptr;
  34.     char * iRptr;
  35.     object iRblk;
  36.     object iWblk;
  37.     int iRblock;
  38.     int iWblock;
  39.     struct _Pipe_iv_t * iNext;
  40. }    Pipe_iv_t;
  41.  
  42.  
  43. #line 44 "pipe.c"
  44. typedef struct  _Pipe_cv_t  {
  45.     struct _Pipe_iv_t * cMpl;
  46. }    Pipe_cv_t;
  47.  
  48. static    Pipe_cv_t    *Pipe_cv;
  49.  
  50.  
  51.  
  52. #line 38 "pipe.d"
  53. cmeth objrtn New(object self, char *name, int bufsiz)
  54.     object obj = oSuper(Pipe_c, gNew, self)(self); 
  55.     ivType *iv = ivPtr(obj); 
  56.  
  57.     iv->iObj = obj; 
  58.     if (name) { 
  59.         iv->iName = Tnalloc(char, strlen(name)+1); 
  60.         strcpy(iv->iName, name); 
  61.     } 
  62.     iv->iBuf = Tnalloc(char, bufsiz); 
  63.     iv->iBufsiz = bufsiz; 
  64.     iv->iWptr = iv->iRptr = iv->iBuf; 
  65.     iv->iNext = Pipe_cv->cMpl; 
  66.     Pipe_cv->cMpl = iv; 
  67.     return obj; 
  68.  
  69. cmeth objrtn Pipe_cm_gNew(object self)
  70.     return New(self, NULL, 128); 
  71.  
  72. imeth int Pipe_im_gWrite(object self, char *buf, unsigned sz)
  73. { Pipe_iv_t *iv = GetIVs(Pipe, self);
  74.     int room, rroom, bytes, w=0; 
  75.  
  76.     if (iv->iWblk) 
  77.         return 0; 
  78.     while (sz) { 
  79.  
  80.  
  81.         room = iv->iBufsiz - (iv->iWptr - iv->iBuf); 
  82.  
  83.  
  84.  
  85.         if (room < (int) sz) { 
  86.             rroom = iv->iRptr - iv->iBuf; 
  87.             if (rroom) { 
  88.                 bytes = iv->iWptr - iv->iRptr; 
  89.                 memmove(iv->iBuf, iv->iRptr, bytes); 
  90.                 iv->iRptr = iv->iBuf; 
  91.                 iv->iWptr = iv->iBuf + bytes; 
  92.                 room = iv->iBufsiz - (iv->iWptr - iv->iBuf); 
  93.             } 
  94.         } 
  95.  
  96.         bytes = (int) sz > room ? room : (int) sz; 
  97.         if (bytes) { 
  98.             memcpy(iv->iWptr, buf, bytes); 
  99.             iv->iWptr += bytes; 
  100.             buf += bytes; 
  101.             sz -= bytes; 
  102.             w += bytes; 
  103.             if (iv->iRblk) 
  104.                 gRelease(iv->iRblk, 0); 
  105.         } 
  106.  
  107.  
  108.         room = iv->iBufsiz - (iv->iWptr - iv->iRptr); 
  109.         if (sz && !room && iv->iWblock) { 
  110.             iv->iWblk = gFindStr(Thread, NULL); 
  111.             gHold(iv->iWblk); 
  112.             iv->iWblk = NULL; 
  113.         } 
  114.         if (!iv->iWblock) 
  115.             break; 
  116.     } 
  117.     return(w); 
  118.  
  119. imeth int Pipe_im_gRead(object self, char *buf, unsigned sz)
  120. { Pipe_iv_t *iv = GetIVs(Pipe, self);
  121.     int ba; 
  122.     int bg; 
  123.     int tr=0; 
  124.  
  125.     if (iv->iRblk) 
  126.         return 0; 
  127.     while (sz) { 
  128.         ba = iv->iWptr - iv->iRptr; 
  129.         bg = ba < (int) sz ? ba : (int) sz; 
  130.         if (bg) { 
  131.             memcpy(buf, iv->iRptr, bg); 
  132.             buf += bg; 
  133.             iv->iRptr += bg; 
  134.             sz -= bg; 
  135.             tr += bg; 
  136.             if (iv->iWblk) 
  137.                 gRelease(iv->iWblk, 0); 
  138.         } 
  139.  
  140.         ba = iv->iWptr - iv->iRptr; 
  141.         if (sz && !ba && iv->iRblock) { 
  142.             iv->iRblk = gFindStr(Thread, NULL); 
  143.             gHold(iv->iRblk); 
  144.             iv->iRblk = NULL; 
  145.         } 
  146.         if (!iv->iRblock) 
  147.             break; 
  148.     } 
  149.     return(tr); 
  150.  
  151. imeth char * Pipe_im_gGets(object self, char *buf, int sz)
  152. { Pipe_iv_t *iv = GetIVs(Pipe, self);
  153.     int ba; 
  154.     int bg; 
  155.     int tr=0; 
  156.  
  157.     if (!(iv->iWptr - iv->iRptr) && !iv->iRblock || iv->iRblk || sz <= 0) 
  158.         return NULL; 
  159.     if (sz-- == 1) { 
  160.         *buf = '\0'; 
  161.         return buf; 
  162.     } 
  163.     while (sz) { 
  164.         ba = iv->iWptr - iv->iRptr; 
  165.         for (bg=0 ; bg < ba && bg < sz && iv->iRptr[bg++] != '\n' ; ); 
  166.         if (iv->iRptr[bg-1] == '\n') 
  167.             sz = bg; 
  168.         if (bg) { 
  169.             memcpy(buf, iv->iRptr, bg); 
  170.             buf += bg; 
  171.             iv->iRptr += bg; 
  172.             sz -= bg; 
  173.             tr += bg; 
  174.             if (iv->iWblk) 
  175.                 gRelease(iv->iWblk, 0); 
  176.         } 
  177.  
  178.         ba = iv->iWptr - iv->iRptr; 
  179.         if (sz && !ba && iv->iRblock) { 
  180.             iv->iRblk = gFindStr(Thread, NULL); 
  181.             gHold(iv->iRblk); 
  182.             iv->iRblk = NULL; 
  183.         } 
  184.         if (!iv->iRblock) 
  185.             break; 
  186.     } 
  187.     buf[tr] = '\0'; 
  188.     return buf; 
  189.  
  190. imeth objrtn Pipe_im_gDispose(object self)
  191. { Pipe_iv_t *iv = GetIVs(Pipe, self);
  192.     ivType *t, *pt; 
  193.  
  194.     if (iv->iRblk || iv->iWblk) 
  195.         return NULL; 
  196.     for (t=Pipe_cv->cMpl, pt=NULL ; t ; pt=t, t=t->iNext) 
  197.         if (t == iv) { 
  198.         if (pt) 
  199.             pt->iNext = t->iNext; 
  200.         else 
  201.             Pipe_cv->cMpl = t->iNext; 
  202.         break; 
  203.     } 
  204.     if (iv->iName) { 
  205.         free(iv->iName); 
  206.         iv->iName = NULL; 
  207.     } 
  208.     if (iv->iBuf) { 
  209.         free(iv->iBuf); 
  210.         iv->iBuf = NULL; 
  211.     } 
  212.     return oSuper(Pipe_c, gDispose, self)(self); 
  213.  
  214. cmeth objrtn Pipe_cm_gFindStr(object self, char *name)
  215.     ivType *p; 
  216.  
  217.     USE(self); 
  218.     if (!name) 
  219.         return NULL; 
  220.     for (p=Pipe_cv->cMpl ; p ; p=p->iNext) 
  221.         if (p->iName && !strcmp(p->iName, name)) 
  222.         return p->iObj; 
  223.     return NULL; 
  224.  
  225. imeth long Pipe_im_gLength(object self)
  226. { Pipe_iv_t *iv = GetIVs(Pipe, self);
  227. #if 0 
  228.         if (iv->iRblock && !(iv->iWptr - iv->iRptr)) { 
  229.         iv->iRblk = gFindStr(Thread, NULL); 
  230.         gHold(iv->iRblk); 
  231.         iv->iRblk = NULL; 
  232.     } 
  233. #endif 
  234.         return iv->iWptr - iv->iRptr; 
  235.  
  236. imeth int Pipe_im_gRoom(object self)
  237. { Pipe_iv_t *iv = GetIVs(Pipe, self);
  238. #if 0 
  239.         if (iv->iWblock && !(iv->iBufsiz - (iv->iWptr - iv->iRptr))) { 
  240.         iv->iWblk = gFindStr(Thread, NULL); 
  241.         gHold(iv->iWblk); 
  242.         iv->iWblk = NULL; 
  243.     } 
  244. #endif 
  245.         return iv->iBufsiz - (iv->iWptr - iv->iRptr); 
  246.  
  247. imeth int Pipe_im_gSize(object self)
  248. { Pipe_iv_t *iv = GetIVs(Pipe, self);
  249.     return iv->iBufsiz; 
  250.  
  251. imeth char * Pipe_im_gName(object self)
  252. { Pipe_iv_t *iv = GetIVs(Pipe, self);
  253.     return iv->iName; 
  254.  
  255. imeth objrtn Pipe_im_gMode(object self, int rblock, int wblock)
  256. { Pipe_iv_t *iv = GetIVs(Pipe, self);
  257.     iv->iRblock = rblock; 
  258.     iv->iWblock = wblock; 
  259.     return self; 
  260.  
  261. imeth long Pipe_im_gAdvance(object self, long sz)
  262. { Pipe_iv_t *iv = GetIVs(Pipe, self);
  263.     int ba; 
  264.     int bg; 
  265.     long tr=0; 
  266.  
  267.     if (iv->iRblk) 
  268.         return 0; 
  269.     while (sz) { 
  270.         ba = iv->iWptr - iv->iRptr; 
  271.         bg = (long) ba < sz ? ba : (int) sz; 
  272.         if (bg) { 
  273.             iv->iRptr += bg; 
  274.             sz -= bg; 
  275.             tr += bg; 
  276.             if (iv->iWblk) 
  277.                 gRelease(iv->iWblk, 0); 
  278.         } 
  279.  
  280.         ba = iv->iWptr - iv->iRptr; 
  281.         if (sz && !ba && iv->iRblock) { 
  282.             iv->iRblk = gFindStr(Thread, NULL); 
  283.             gHold(iv->iRblk); 
  284.             iv->iRblk = NULL; 
  285.         } 
  286.         if (!iv->iRblock) 
  287.             break; 
  288.     } 
  289.     return(tr); 
  290.  
  291. imeth long Pipe_im_gPosition(object self)
  292.     USE(self); 
  293.     return 0L; 
  294.  
  295. imeth int Pipe_im_gEndOfStream(object self)
  296. { Pipe_iv_t *iv = GetIVs(Pipe, self);
  297.     return !(iv->iWptr - iv->iRptr); 
  298.  
  299.  
  300. #line 320 "pipe.c"
  301.  
  302. objrtn    Pipe_initialize(void)
  303. {
  304.     static  CRITICALSECTION  cs;
  305.     static  int volatile once = 0;
  306.  
  307.     ENTERCRITICALSECTION(_CI_CS_);
  308.     if (!once) {
  309.         INITIALIZECRITICALSECTION(cs);
  310.         once = 1;
  311.     }
  312.     LEAVECRITICALSECTION(_CI_CS_);
  313.  
  314.     ENTERCRITICALSECTION(cs);
  315.  
  316.     if (Pipe_c) {
  317.         LEAVECRITICALSECTION(cs);
  318.         return Pipe_c;
  319.     }
  320.     INHIBIT_THREADER;
  321.     Stream_initialize();
  322.     if (Pipe_c)  {
  323.         ENABLE_THREADER;
  324.         LEAVECRITICALSECTION(cs);
  325.         return Pipe_c;
  326.     }
  327.     Pipe_c = gNewClass(Class, "Pipe", sizeof(Pipe_iv_t), sizeof(Pipe_cv_t), Stream, END);
  328.     cMethodFor(Pipe, gNewWithStrInt, New);
  329.     cMethodFor(Pipe, gFindStr, Pipe_cm_gFindStr);
  330.     cMethodFor(Pipe, gNew, Pipe_cm_gNew);
  331.     iMethodFor(Pipe, gPosition, Pipe_im_gPosition);
  332.     iMethodFor(Pipe, gLength, Pipe_im_gLength);
  333.     iMethodFor(Pipe, gWrite, Pipe_im_gWrite);
  334.     iMethodFor(Pipe, gEndOfStream, Pipe_im_gEndOfStream);
  335.     iMethodFor(Pipe, gAdvance, Pipe_im_gAdvance);
  336.     iMethodFor(Pipe, gSize, Pipe_im_gSize);
  337.     iMethodFor(Pipe, gDispose, Pipe_im_gDispose);
  338.     iMethodFor(Pipe, gName, Pipe_im_gName);
  339.     iMethodFor(Pipe, gGets, Pipe_im_gGets);
  340.     iMethodFor(Pipe, gGCDispose, Pipe_im_gDispose);
  341.     iMethodFor(Pipe, gDeepDispose, Pipe_im_gDispose);
  342.     iMethodFor(Pipe, gRead, Pipe_im_gRead);
  343.     iMethodFor(Pipe, gRoom, Pipe_im_gRoom);
  344.     iMethodFor(Pipe, gMode, Pipe_im_gMode);
  345.  
  346.     Pipe_cv = GetCVs(Pipe);
  347.  
  348.     ENABLE_THREADER;
  349.  
  350.     LEAVECRITICALSECTION(cs);
  351.  
  352.     return Pipe_c;
  353. }
  354.  
  355.  
  356.  
  357.